
#include <stdio.h>
#include <cstring>
#include "userTasks.h"
#include "GW_ADChannel.h"
#include "GW_Conf.h"
#include "GW_Rs485.h"
#include "GW_ModProtocol.h"
#include "cmsis_os.h"

extern ADC_HandleTypeDef hadc;
extern DMA_HandleTypeDef hdma_adc;

extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim14;

extern osSemaphoreId recvSemHandle;

GW_RS485 com_transmit;
GW_ModProtocol pol_Transmit;
GW_ADChannel channels[CONF_CHANNEL_CNT + 1];
u32 adcbuf[CONF_CHANNEL_CNT];
u16   outbuf[CONF_CHANNEL_CNT];

u8 str[]="ok";
void task_do_transmit()
{
   bool ret = false;
   u8* volatile txbuf;
   int len=0;
   com_transmit.config(&huart1, &htim6,&hdma_usart1_rx,&recvSemHandle);
   com_transmit.enableIT();
   while (1)
   {
      osSemaphoreWait(recvSemHandle, osWaitForever);
      //HAL_UART_Transmit(&huart1,str,2,100);
      ret = pol_Transmit.Matching(hdma_usart1_rx.Instance->CNDTR, com_transmit.recvbuf, (u8)CONF_ID, DIR_REQUEST);
      if (ret)
      {
         ret = pol_Transmit.Recved((u8 *)outbuf, CONF_CHANNEL_CNT, CONF_ID);
         if (ret)
         {
            txbuf = pol_Transmit.GetSendMsg(&len);
            HAL_UART_Transmit_DMA(&huart1,txbuf,len);
            osDelay(10); 
         }
      }
   }
   
}

void task_do_adc()
{
  int i=0;
   HAL_ADCEx_Calibration_Start(&hadc);
   HAL_ADC_Start_DMA(&hadc, adcbuf, CONF_CHANNEL_CNT);
   for (i = 0; i < CONF_CHANNEL_CNT; i++)
   {
      channels[i].setptAD(adcbuf + i);
      channels[i].setptOUT(outbuf + i);
   }
   while (1)
   {
      for (i = 0; i < CONF_CHANNEL_CNT; i++)
      {
         channels[i].reflesh();
         channels[i].getChannelAD();
         osDelay(10);
      }
   }
}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
   com_transmit.beginRecv();
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
   com_transmit.timeout();
}
//void task_do_HMI()
//{
//   int cnt = 0;
//   int changed = 0;
//   bool res = false;
//   while (1)
//   {
//          osSemaphoreWait(Sem_HMICOMHandle, osWaitForever);
//          cnt = com_hmi->recvCount();
//          if (cnt > 0)
//          {
//                 res = pol_HMIM.Matching(cnt, com_hmi->recvbuf, CGlobal::addr, DIR_REQUEST);
//                 if (res == true)
//                 {
//                        HAL_GPIO_WritePin(LED_OK_GPIO_Port, LED_OK_Pin, GPIO_PIN_SET);
//                        switch (pol_HMIM.GetFunctionID())
//                        {
//                        case MODBUS_WRITEHOLD:
//                           {
//                                  pol_HMIM.Recved(MEM.MEM32_Buf, sizeof(MEM), CGlobal::addr);
//                                  com_hmi->write(pol_HMIM.sendbuf, pol_HMIM.sendLength);
//                                  changed = memcmp(MEM.MEM32.protocol, oldprotocol, sizeof(oldprotocol));
//                                  if (changed != 0)
//                                  {
//                                         memcpy(oldprotocol, MEM.MEM32.protocol, sizeof(oldprotocol));
//                                         protocolChanged();
//                                  }
//                                  osTimerStart(myTimerWatchHMIHandle, 100);
//                                  break;
//                           }
//                        case MODBUS_INPUTREG:
//                           {
//                                  pol_HMIM.Recved((u8 *)outbuf, 64, CGlobal::addr);
//                                  com_hmi->write(pol_HMIM.sendbuf, pol_HMIM.sendLength);
//                                  osTimerStart(myTimerWatchHMIHandle, 100);
//                                  break;
//                           }
//                        }
//                 }
//                 //osDelay(10);
//                 //HAL_GPIO_WritePin(LED_OK_GPIO_Port, LED_OK_Pin, GPIO_PIN_RESET);
//          }
//     GUI_Delay(50);
//   }
//}

